<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="decorationDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="模版名称" prop="templateName">
							<el-input v-model="state.ruleForm.templateName" placeholder="请输入按钮名称" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入备注" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(decorationDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="DecorationDialog">
import { reactive, ref, nextTick } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import msg from '/@/utils/messageUtils';
import { decorationApi } from '/@/api/shop/decoration';
import { stat } from 'fs';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

let decorationDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '0',
		templateName: '',
		remark: '',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const rules = reactive<FormRules>({
	templateName: [{ required: true, message: '请输入模版名称', trigger: 'blur' }],
});

const openDialog = (type: string, id: string) => {
	if (type === 'edit') {
		state.dialog.title = '修改模版';
		state.dialog.submitTxt = '修 改';
		state.dialog.type = 'edit';
		viewDecoration(id);
	} else {
		state.dialog.title = '新增模版';
		state.dialog.submitTxt = '新 增';
		state.dialog.type = type;
		nextTick(() => {
			decorationDialogFormRef.value.resetFields();
		});
	}
	state.dialog.isShowDialog = true;
};

const viewDecoration = async (id: string) => {
	try {
		const { data } = await decorationApi().view(id);
		state.ruleForm.remark = data.remark;
		state.ruleForm.id = data.id;
		state.ruleForm.templateName = data.name;
	} catch (error) {
		msg().error('获取模版详细失败，请重试');
	}
};

let onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate(async (valid) => {
		if (valid) {
			try {
				if (state.dialog.type === 'add') {
					await decorationApi().create({
						name: state.ruleForm.templateName,
						remark: state.ruleForm.remark,
					});
				} else {
					await decorationApi().update({
						name: state.ruleForm.templateName,
						remark: state.ruleForm.remark,
						id: state.ruleForm.id,
					});
				}
				msg().success('操作成功');
				onCancel();
				emit('refresh');
			} catch (error) {
				msg().error(error);
			}
		}
	});
};

let onCancel = () => {
	state.dialog.isShowDialog = false;
};

defineExpose({
	openDialog,
});
</script>

<style lang="scss" scoped></style>
